package com.order.rest;

import com.alibaba.fastjson.JSON;

import com.order.service.OrderService;
import com.order.exception.CustomException;
import com.order.entity.Order;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author
 * @PathVariable("xxx")，通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“)
 */
@RestController
public class OrderRest {
	@Autowired
	private OrderService orderService;
	private Logger logger = LoggerFactory.getLogger(getClass());

	@ApiOperation(value = "插入", httpMethod = "POST")
	@ApiImplicitParam(name = "SessionId", value = "SessionId", required = true, dataType = "string", paramType = "header")
	@ApiResponses({
			@ApiResponse(code = 400, message = "SessionId未通过"),
			@ApiResponse(code = 1000, message = "no重复")
	})
	@RequestMapping(value = "/v1/orders", method = RequestMethod.POST)

	public Map<String, Object> insert(
			@RequestHeader(value = "SessionId") String sessionId,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody Order entity)throws CustomException {
		logger.info("插入数据为:{}", JSON.toJSONStringWithDateFormat(entity,"yyyy-MM-dd HH:mm:ss"));
		return orderService.insert(entity,sessionId);
	}



	@ApiOperation(value = "向kafka传送订单", httpMethod = "POST")
	@ApiImplicitParam(name = "SessionId", value = "SessionId", required = true, dataType = "string", paramType = "header")
	@ApiResponses({
			@ApiResponse(code = 400, message = "SessionId未通过"),
			@ApiResponse(code = 1000, message = "no重复")
	})
	@RequestMapping(value = "/v2/orders", method = RequestMethod.POST)

	public Map<String, Object> send(
			@RequestHeader(value = "SessionId") String sessionId,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody Order entity)throws CustomException {
		logger.info("插入数据为:{}", JSON.toJSONStringWithDateFormat(entity,"yyyy-MM-dd HH:mm:ss"));
		return orderService.send(entity,sessionId);
	}



	@ApiOperation(value = "删除", httpMethod = "DELETE")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "no", value = "No", required = true, dataType = "int", paramType = "path"),
			@ApiImplicitParam(name = "SessionId", value = "SessionId", required = true, dataType = "string", paramType = "header")
	})
	@ApiResponses({
			@ApiResponse(code = 400, message = "SessionId未通过"),
			@ApiResponse(code = 14001, message = "no不存在")
	})
	@RequestMapping(value = "/v1/orders/{no}", method = RequestMethod.DELETE)
	public Map<String, Object> delete(
			@PathVariable("no") Integer no,
			@RequestHeader(value = "SessionId") String sessionId,
			HttpServletRequest request,
			HttpServletResponse response)throws CustomException {
		logger.info("删除no:{}",no);
		return orderService.delete(no,sessionId);
	}




	@ApiOperation(value = "修改", httpMethod = "PUT")
	@ApiImplicitParam(name = "SessionId", value = "SessionId", required = true, dataType = "string", paramType = "header")
	@ApiResponses({
			@ApiResponse(code = 400, message = "SessionId未通过"),
			@ApiResponse(code = 14001, message = "no不存在")
	})
	@RequestMapping(value = "/v1/orders", method = RequestMethod.PUT)
	public Map<String, Object> update(
			@RequestHeader(value = "SessionId") String sessionId,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestBody Order entity)throws CustomException {
		logger.info("修改数据为:{}",JSON.toJSONStringWithDateFormat(entity,"yyyy-MM-dd HH:mm:ss"));
		return orderService.update(entity,sessionId);
	}




	@ApiOperation(value = "按no查询", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "no", value = "No", required = true, dataType = "int", paramType = "path"),
			@ApiImplicitParam(name = "SessionId", value = "SessionId", required = true, dataType = "string", paramType = "header")
	})
	@ApiResponses({
			@ApiResponse(code = 400, message = "参数不正确")
	})
	@RequestMapping(value = "/v1/orders/{no}", method = RequestMethod.GET)
	public Map<String, Object> select(
			@PathVariable("no") Integer no,
			@RequestHeader(value = "SessionId") String sessionId)throws CustomException {
		logger.info("按no查询为:{}",no);
		logger.info("查询结果为:{}",orderService.selectByNo(no,sessionId));
		return orderService.selectByNo(no,sessionId);

	}






}
